package com.colin.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by colinx on 2017/8/4.
 */
public class DecodeUtils {

    public static byte[] decode(String key){
        return Base64.decodeBase64(key.getBytes());
    }

    public static String encodeBytes(byte[] bytes){
        return new String(Base64.encodeBase64(bytes));
    }

    public String getBytes(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return encodeBytes(buffer);
    }

    public static void main(String[] args) {
        DecodeUtils utils=new DecodeUtils();
        String rr=utils.getBytes(new File("e://testws.pkcs12"));
        System.out.println(rr);
    }
}
